<?php

namespace App\Api\Controllers\v1;

use App\Http\Controllers\ApiController;
use App\Http\Response\ApiResponse;
use App\Models\AgentUser;
use App\Models\Card;

use App\Models\PaymentOrder;
use App\Models\ShopUser;
use App\Models\UserCar;
use App\Models\UserCard;
use App\Services\OrderService;
use hg\apidoc\annotation as Apidoc;
use Illuminate\Http\Request;

/**
 * 洗车卡接口
 * @Apidoc\Group("user")
 * @Apidoc\Sort(1)
 */
class CardController extends ApiController
{

    /**
     * 洗车卡列表-改,去掉经纬度参数
     * @Apidoc\Url("/api/v1/card/index")
     * @Apidoc\Method("GET")
     * @Apidoc\Query("shop_id", type="int", desc="门店ID")
     * @Apidoc\Query(ref="queryPage")
     * @Apidoc\Returned("items",type="array",ref="App\Models\Card")
     * @Apidoc\Returned("total",type="int",desc="总条数")
     * @Apidoc\Returned("lastPage",type="int",desc="最大页码")
     */
    public function index(): ApiResponse
    {
        $data = Card::getList();
        return $this->response()->data($data);
    }

    /**
     * 洗车卡详情
     * @Apidoc\Url("/api/v1/card/detail/{id}")
     * @Apidoc\Method("GET")
     * @Apidoc\RouteParam("id",type="int", require=true, desc="id参数", default="0")
     * @Apidoc\Returned("item",type="array",ref="App\Models\Card")
     */
    public function detail(Request $request): ApiResponse
    {
        $user_id = $this->user_id;
        $id = $request->route("id");
        $data = Card::getDetail($id, $user_id);
        return $this->response()->data($data);
    }



    /**
     * 用户的洗车卡列表
     * @Apidoc\Url("/api/v1/card/list")
     * @Apidoc\Method("GET")
     * @Apidoc\Query("type",type="int", require=true, desc="0=可使用,1=不可使用", default="0")
     * @Apidoc\Query(ref="queryPage")
     * @Apidoc\Returned("items",type="array",ref="App\Models\UserCard")
     * @Apidoc\Returned("total",type="int",desc="总条数")
     * @Apidoc\Returned("lastPage",type="int",desc="最大页码")
     */
    public function list(): ApiResponse
    {
        $user_id = $this->user_id;
        $data = UserCard::getList($user_id);
        return $this->response()->data($data);
    }

    /**
     * 用户的洗车卡详情
     * @Apidoc\Url("/api/v1/card/getDetail/{id}")
     * @Apidoc\Method("GET")
     * @Apidoc\RouteParam("id",type="int", require=true, desc="id参数", default="0")
     * @Apidoc\Returned("items",type="array",ref="App\Models\UserCard")
     */
    public function getDetail(Request $request): ApiResponse
    {
        $user_id = $this->user_id;
        $id = $request->route("id");
        $data = UserCard::getDetail($id, $user_id);
        return $this->response()->data($data);
    }

    /**
     * 根据经纬度和服务获取的用户可使用的洗车卡列表-改,去掉经纬度参数
     * @Apidoc\Url("/api/v1/card/allows")
     * @Apidoc\Method("GET")
     * @Apidoc\Query("service_id",type="int", require=true, desc="服务ID", default="")
     * @Apidoc\Query("car_id",type="int", require=true, desc="车辆ID", default="")
     * @Apidoc\Returned("items",type="array",ref="App\Models\UserCard")
     */
    public function allows(): ApiResponse
    {
        $user_id = $this->user_id;
        $data['items'] = UserCard::allows($user_id);
        return $this->response()->data($data);
    }

    /**
     * 洗车卡购买支付
     * @Apidoc\Url("/api/v1/card/pay")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("card_id", type="int", require="true", desc="洗车卡ID",  default="0")
     * @Apidoc\Param("car_id", type="int", require="true", desc="绑定的车辆ID",  default="0")
     */
    public function pay(){
        $card_id = request()->post("card_id");
        $car_id = request()->post("car_id");
        $card = Card::query()->where("id",$card_id)->first();
        if (!$card){
            return $this->response()->error("请选择洗车卡");
        }
        $car = UserCar::query()->where("id",$car_id)->first();
        if (!$car){
            return $this->response()->error("请选择绑定的车辆");
        }
        $extend['card'] = $card;
        $extend['shop_id'] = $card->shop_id;
        $extend['car'] = $car;
        return $this->wechatPay(function ($out_trade_no,$openid,$user_id) use($extend) {
            $title = $extend['card']->title;
            $amount = floatval($extend['card']->price);
            OrderService::addCardOrder($user_id,$out_trade_no,$amount,$extend);
            $notify_url = route("api.v1.notify",['type'=>3]);
//            $paymentOrder = PaymentOrder::query()->where('out_trade_no',$out_trade_no)
//                ->where("pay_status",0)
//                ->whereNull('pay_at')->first();
//            PaymentOrder::updateOrder([
//                "out_trade_no"=>$out_trade_no,
//                "transaction_id"=>"1232456566"
//            ],$paymentOrder,3);
            $body = "购买".$title;
            return PaymentOrder::getPayData($openid, $body, $out_trade_no, $amount, $notify_url);
        });
    }

}
